import { createStore, applyMiddleware, compose } from 'redux'
import promiseMiddleware from 'redux-promise'
import { makeAllReducer } from './reducerUtils'

export const setStore = (initialReducer = {}) => {
  const middlewares = [promiseMiddleware]
  const enhancers = []
  const composeEnhancers =
    typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
      ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
      : compose

  if (process.env.NODE_ENV === 'development') {
    middlewares.push(require('redux-logger').createLogger())
  }

  const enhancer = composeEnhancers(applyMiddleware(...middlewares), ...enhancers)
  let store = createStore(makeAllReducer(initialReducer), enhancer)

  store.asyncReducers = {
    ...initialReducer
  }

  return store
}
